package com.fresh.bus.chat.ctrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;

import com.fresh.bus.canoe.command.annotation.CanoeCommand;
import com.fresh.bus.canoe.message.Request;
import com.fresh.bus.canoe.message.Response;
import com.fresh.bus.canoe.session.Session;
import com.fresh.bus.chat.business.WealthService;
import com.fresh.bus.chat.util.MessageUtils;

@Controller("qqFriendCtrl")
public class QQFriendCtrl extends BaseCtrl {
	@CanoeCommand(value = OpCodes.SHOW_QQ_FRIEND)
	public Response showFriend(Session session, Request request) {
		Map<String, Object> data = (Map<String, Object>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.SHOW_QQ_FRIEND);
		try {
			Map<String, String> user = this.userService.findUser(id);
			boolean flag = !user.get("qqNo").equals("");
			response.setData(flag);
		} catch (Exception e) {
			log.error("User[" + id + "]", e);
			response.setData(e.getMessage());
		}
		return response;
	}

	@CanoeCommand(value = OpCodes.SETTING_QQ_FRIEND)
	public Response friendSetting(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.SETTING_QQ_FRIEND);
		try {
			this.friendService.setFriendCond(data, id);
			response.setData(data);
		} catch (Exception e) {
			log.error("User[" + id + "]", e);
			response.setData(e.getMessage());
		}
		return response;
	}

	@CanoeCommand(value = OpCodes.REGISTER_QQ)
	public Response registerNo(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.REGISTER_QQ);
		try {
			String qqNo = data.get("c");
			this.friendService.setQQNo(qqNo, id);
			this.expService.update(session, null, OpCodes.REGISTER_QQ);
			this.wealthService.update(1,WealthService.REGISTER_QQ, id);
			this.userService.synchroUser(session);
			response.setData(true);
			return response;
		} catch (Exception e) {
			log.error("User[" + id + "]", e);
		}
		return null;
	}
	
	@CanoeCommand(value = OpCodes.REQ_QQ_NO)
	public Response requestNo(Session session, Request request) {
		Map<String, String> data = (Map<String, String>) request.getData();
		String id = session.getRole().getId();
		Response response = new Response(OpCodes.REQ_QQ_NO);
		try {
			String user = data.get("u");
			String qqNo = this.friendService.getQQNo(user, id);
			response.setData(qqNo);
		} catch (Exception e) {
			Map<String,String> map = new HashMap<String,String>();
			map.put("error", "error");
			map.put("msg", e.getMessage());
			response.setData(map);
		}
		return response;
	}	

}
